<?xml version="1.0" encoding="UTF-8" ?>

<!--
    Document   : atom2tree-rdf.xsl
    Created on : April 30, 2006, 7:37 AM
    Author     : alex
    Description:
        Purpose of transformation follows.
-->

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" 
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:atomic="http://www.atomojo.org/O/atomic/"
   xmlns:atom="http://www.w3.org/2005/Atom">
   
<xsl:param name="root"/>

<xsl:template match="/">
   <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
            xmlns:atomic="http://www.atomojo.org/O/atomic/">
      <xsl:text>&#xa;</xsl:text>
      <rdf:Seq rdf:about="{$root}">
         <!--
         <xsl:apply-templates select="atom:feed/*[namespace-uri(.)!='http://www.w3.org/2005/Atom' or local-name(.)!='entry']"
                              mode="root"/>
                              -->
         <!--
         <rdf:li rdf:resource="{$root}/entries"/>
         -->
         <xsl:apply-templates select="atom:feed/atom:entry" mode="root"/>
      </rdf:Seq>
      <xsl:text>&#xa;</xsl:text>
         <!--
      <rdf:Description rdf:about="{$root}/entries" atomic:title="Entries" atomic:id="" atomic:type="" atomic:icon="entries"/>
      <rdf:Seq rdf:about="{$root}/entries">
         <xsl:apply-templates select="atom:feed/atom:entry" mode="root"/>
      </rdf:Seq>
      -->
      <xsl:apply-templates/>
   </rdf:RDF>
</xsl:template>

<xsl:template match="atom:feed" mode="root">
   <xsl:apply-templates select="*" mode="root"/>
</xsl:template>
<xsl:template match="atom:feed">
   <xsl:apply-templates select="*"/>
</xsl:template>

<xsl:template match="atom:feed/atom:link[@rel='edit' or @rel='self']" mode="root" priority="2"/>
<xsl:template match="atom:feed/atom:link[@rel='edit' or @rel='self']" priority="2"/>

<xsl:template match="atom:feed/*" mode="root">
   <rdf:li rdf:resource="{../atom:id}/element/{position()}"/>
   <xsl:text>&#xa;</xsl:text>
</xsl:template>
<xsl:template match="atom:feed/atom:*" priority="2">
   <!--
   <rdf:Description rdf:about="{../atom:id}/element/{position()}" 
                    atomic:id="{../atom:id}"
                    atomic:type="feed-{local-name(.)}" 
                    atomic:position="{position()}" 
                    atomic:title="{{{namespace-uri(.)}}}{local-name(.)}"
                    >
      <xsl:call-template name="make-title"/>
   </rdf:Description>
   <xsl:text>&#xa;</xsl:text>
   -->
</xsl:template>
<xsl:template match="atom:feed/*">
   <rdf:Description rdf:about="{../atom:id}/element/{position()}" 
                    atomic:id="{../atom:id}"
                    atomic:type="feed-other" 
                    atomic:position="{position()}" 
                    atomic:title="{{{namespace-uri(.)}}}{local-name(.)}"
                    >
      <xsl:call-template name="make-title"/>
   </rdf:Description>
   <xsl:text>&#xa;</xsl:text>
</xsl:template>

<xsl:template name="make-title">
   <xsl:choose>
      <xsl:when test="namespace-uri(.)='http://www.w3.org/2005/Atom'">
         <xsl:choose>
            <xsl:when test="local-name(.)='link'">
               <xsl:attribute name="atomic:title">[rel <xsl:value-of select="@rel"/><xsl:text>] </xsl:text>
                  <xsl:if test="@type">[type <xsl:value-of select="@type"/>] </xsl:if>
                  <xsl:value-of select="@href"/>
               </xsl:attribute>
            </xsl:when>
            <xsl:when test="local-name(.)='category'">
               <xsl:choose>
                  <xsl:when test="@scheme">
                     <xsl:attribute name="atomic:title">[scheme <xsl:value-of select="@scheme"/>] [term <xsl:value-of select="@term"/>] <xsl:value-of select="."/></xsl:attribute>
                  </xsl:when>
                  <xsl:otherwise>
                     <xsl:attribute name="atomic:title">[term <xsl:value-of select="@term"/>] <xsl:value-of select="."/></xsl:attribute>
                  </xsl:otherwise>
               </xsl:choose>
            </xsl:when>
            <xsl:when test="local-name(.)='id'">
               <xsl:attribute name="atomic:title">ID: <xsl:value-of select="."/>
               </xsl:attribute>
            </xsl:when>
            <xsl:otherwise>
               <xsl:attribute name="atomic:title"><xsl:value-of select="."/></xsl:attribute>
            </xsl:otherwise>
         </xsl:choose>
      </xsl:when>
      <xsl:otherwise>
         <xsl:attribute name="atomic:title"><xsl:value-of select="name(.)"/>: <xsl:value-of select="."/></xsl:attribute>
      </xsl:otherwise>
   </xsl:choose>
</xsl:template>

<xsl:template match="atom:entry" mode="root" priority="2">
   <rdf:li rdf:resource="{atom:id}"/>
   <xsl:text>&#xa;</xsl:text>
</xsl:template>

<xsl:template match="atom:entry" priority="2">
   <rdf:Description rdf:about="{atom:id}" atomic:id="{atom:id}" atomic:title="{atom:title}"  atomic:type="{local-name(.)}"/>
   <xsl:text>&#xa;</xsl:text>
   <rdf:Seq rdf:about="{atom:id}">
      <xsl:apply-templates select="*" mode="entry-li"/>
   </rdf:Seq>
   <xsl:apply-templates select="*" mode="entry"/>
</xsl:template>

<!--
<xsl:template match="atom:entry/atom:id" mode="entry-li" priority="2"/>
<xsl:template match="atom:entry/atom:id" mode="entry" priority="2"/>
-->
<xsl:template match="atom:entry/atom:title" mode="entry-li" priority="2"/>
<xsl:template match="atom:entry/atom:title" mode="entry" priority="2"/>
<xsl:template match="atom:entry/atom:link[@rel='edit' or @rel='self']" mode="entry-li" priority="2"/>
<xsl:template match="atom:entry/atom:link[@rel='edit' or @rel='self']" mode="entry" priority="2"/>


<xsl:template match="atom:entry/atom:published" priority="2" mode="entry-li"/>
<xsl:template match="atom:entry/*" mode="entry-li">
   <rdf:li rdf:resource="{../atom:id}/element/{position()}"/>
   <xsl:text>&#xa;</xsl:text>
</xsl:template>
<xsl:template match="atom:entry/atom:*" mode="entry" priority="2">
   <rdf:Description rdf:about="{../atom:id}/element/{position()}" 
                    atomic:id="{../atom:id}"
                    atomic:type="{local-name(.)}" 
                    atomic:position="{position()}" 
                    atomic:title="{{{namespace-uri(.)}}}{local-name(.)}">
      <xsl:call-template name="make-title"/>
   </rdf:Description>
   <xsl:text>&#xa;</xsl:text>
</xsl:template>
<xsl:template match="atom:entry/atom:updated" priority="2" mode="entry">
   <rdf:Description rdf:about="{../atom:id}/element/{position()}" 
                    atomic:id="{../atom:id}"
                    atomic:type="other" 
                    atomic:position="{position()}" 
                    atomic:title="updated {substring-before(.,'T')} {substring-after(.,'T')}, created on {substring-before(../atom:published,'T')} {substring-after(../atom:published,'T')}"
                    >
   </rdf:Description>
   <xsl:text>&#xa;</xsl:text>
</xsl:template>
<xsl:template match="atom:entry/atom:published" priority="2" mode="entry"/>
<xsl:template match="atom:entry/atom:content[@src]" priority="3" mode="entry">
   <rdf:Description rdf:about="{../atom:id}/element/{position()}" 
                    atomic:id="{../atom:id}"
                    atomic:type="other" 
                    atomic:position="{position()}" 
                    atomic:title="src={@src} [type {@type}]"
                    >
   </rdf:Description>
   <xsl:text>&#xa;</xsl:text>
</xsl:template>
<xsl:template match="atom:entry/atom:content" priority="2" mode="entry">
   <rdf:Description rdf:about="{../atom:id}/element/{position()}" 
                    atomic:id="{../atom:id}"
                    atomic:type="other" 
                    atomic:position="{position()}" 
                    atomic:title="[type {@type}] {.}"
                    >
   </rdf:Description>
   <xsl:text>&#xa;</xsl:text>
</xsl:template>

<xsl:template match="atom:entry/*" mode="entry">
   <rdf:Description rdf:about="{../atom:id}/element/{position()}" 
                    atomic:id="{../atom:id}"
                    atomic:type="other" 
                    atomic:position="{position()}" 
                    >
      <xsl:call-template name="make-title"/>
   </rdf:Description>
   <xsl:text>&#xa;</xsl:text>
</xsl:template>

</xsl:transform>
